/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fwdisp;

import ch.ubique.inieditor.IniEditor;
import java.awt.AWTException;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import settings.MainSettingsManager;

/**
 *
 * @author rbs
 */
public class Main {

    /*
     * @param args the command line arguments
     */

    public static void main(String[] args) throws IOException, AWTException {


        //Monitor anschalten:
        //new Robot().keyPress(KeyEvent.CHAR_UNDEFINED);
        //new Robot().keyRelease(KeyEvent.CHAR_UNDEFINED);
        
        
        MainSettingsManager.load();
        
        File log_folder = new File(getProgramPath() + "log" + File.separator);
        log_folder.mkdirs();  //creates all subfolders
        FileHandler file = new FileHandler (getProgramPath() + "log" + File.separator + "fwdisp.log", true);
       
        file.setFormatter(new SimpleFormatter());
        Logger.getLogger("").addHandler(file);
        Logger.getLogger("").setLevel(Level.ALL);

        Logger logger = Logger.getLogger(Main.class.toString());
        for(String s: args) {
            logger.log(Level.FINE ,"input: " + s);
        }


        String input = null;

        MainFrame frame = new MainFrame();

        if (args.length == 1)
        {
            input = args[0];
        }
            
        else if (args.length > 1)
        {
            if(args[0].equals("--fmslaunch")) //Start durch FMS-Launcher
            {
                input = args[1];
                frame.SetRICs(args[2]);
                //JOptionPane.showMessageDialog(null, args[0] + "\n\r" + args[1] + "\n\r" + args[2]);
            }
            else
            {
            logger.log(Level.FINE, "Bitte nur einen Parameter übergeben:");
            logger.log(Level.FINE, "Datei.jar \"Hier steht der Alarmierungstext\"");
            System.exit(0);
            }
        }
        else
        {
            logger.log(Level.FINE, "Bitte einen Parameter übergeben:");
            logger.log(Level.FINE, "Datei.jar \"Hier steht der Alarmierungstext\"");
            Runtime.getRuntime().exec("java -jar FWDispGuiLauncher.jar");
            System.exit(0);
        }
        Analyser analyse = new Analyser();

        analyse.splitUp(input, MainSettingsManager.getControlCenterSettings().getCenter());

        IniEditor reader = new IniEditor();
        reader.load("settings.ini");

        if(reader.get("updates", "mode") .equals("before"))
        {
           logger.log(Level.FINE, "Updates before program start are not implemented correctly. Skip updates.");
           
        }

        frame.draw();

        Timer timer = new Timer();

        int time = 1000 * 60 * Integer.parseInt(reader.get("autoexit", "time"));
        String get = reader.get("autoexit", "active");
        if(!get.equals("false"))
        {    
            //Exit warner = new Exit(frame, "Ende in 5 min");
            Exit closer = new Exit(frame);
            //timer.schedule(warner, time - 300000); //10 min, TODO: insert times from ini
            timer.schedule(closer, time);
            //frame.addCountDown("Ende in", time);
        }
        if(input.contains("Abbruch") || input.contains("ABBRUCH") || input.contains("Abbruch") || input.contains("Abbrechen") || input.contains("ABBRECHEN"))
        {
            frame.drawButtons(false);
            frame.showBigLabel("EINSATZ ABBRECHEN");
        }
        else
        {
            frame.SetIniSettings();
            
            if (analyse.getOrdnung() != null)
            {
                frame.SetBgColor(analyse.getOrdnung()); //TODO: Change this getEType().getContains()
                frame.SetTexts(analyse.getOrdnung().getEType().getName(), analyse.getEOrt(), analyse.getETxt(), analyse.getENr()); //TODO: Change this getEType().getContains()
                frame.SetStatusColors(analyse.getOrdnung().getId());
                frame.SetTypPicture(analyse.getOrdnung());
            }
            else
            {
                frame.SetBgColor(null);
                frame.SetTexts("", analyse.getEOrt(), analyse.getETxt(), analyse.getENr()); //TODO: Change this getEType().getContains()
            }
                
            frame.drawButtons(true);
        }
        
        
        frame.setVisible(true);
    }

    public static String getProgramPath() {
        
        File file = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().getFile());
        if (file.getName().equals("build"))
            file = file.getParentFile();
        
        System.out.println("Path: "  +  file.getParent());
        
        return file.getParent() + File.separator;
    }
}
